<template>
  <div>
    <el-dialog :title="'选择'+pType" :visible.sync="dialogTableVisible">
      <div class="departSearch">
        <el-input v-model="input" placeholder="请输入内容" class="pop_input" />
        <el-button type="success" class="pop_btn" @click="searchFun">搜索</el-button>
        <el-button type="primary" class="pop_btn" @click="sureFun">确认</el-button>
      </div>
      <Graytable ref="graytable" :t-head="tHead" :t-data="tData" @selectionChange="selectionChange" />
      <pagination :total="query.total" :page.sync="query.page" :limit.sync="query.limit" @pagination="getList" />
    </el-dialog>
  </div>
</template>

<script>
import { GetDrug,
  GetDoctor,
  GetRoute,
  GetDrugType,
  GetDept,
  GetDisease,
  GetOperation,
  GetOperationType,
  GetMedgroup,
  GetDrugDnc } from '@/api/prc17212'
export default {
  name: 'Poptable',
  props: {
    pType: {
      required: true,
      type: String
    },
    ctype: {
      type: String,
      default: 'mz'
    },
    setting: {
      type: Boolean,
      default: false
    },
    hospitalCode: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      query: {
        total: 0,
        page: 1,
        limit: 10
      },
      tHead: [],
      tData: [],
      popFlag: true,
      input: '',
      srm: '',
      type: '',
      selectLists: '',
      title: ''
    }
  },
  watch: {
    pType(e) {
      this.type = this.pType
      this.title = '选择' + this.type
      this.changeFun()
    },
    ctype() {
      this.changeFun()
    }
  },
  methods: {
    changeFun() {
      this.query.page = 1
      this.query.limit = 10
      this.srm = ''
      this.input = ''
      this.query.total = 0
      this.tData = []
      if (this.type === '药品') {
        this.tHead = [{ selection: true }, { prop: 'uniquecode', label: '药品编码' }, { prop: 'drugname', label: '药品' }]
      } else if (this.type === '医生') {
        this.tHead = [{ selection: true }, { prop: 'doctorcode', label: '医生编码' }, { prop: 'doctorname', label: '医生' }]
      } else if (this.type === '给药途径') {
        this.tHead = [{ selection: true }, { prop: 'routecode', label: '给药途径编码' }, { prop: 'routename', label: '给药途径' }]
      } else if (this.type === '科室') {
        this.tHead = [{ selection: true }, { prop: 'deptcode', label: '科室编码' }, { prop: 'deptname', label: '科室' }]
      } else if (this.type === '疾病') {
        this.tHead = [{ selection: true }, { prop: 'discode', label: '疾病编码' }, { prop: 'disname', label: '疾病' }]
      } else if (this.type === '手术') {
        this.tHead = [{ selection: true }, { prop: 'opercode', label: '手术编码' }, { prop: 'opername', label: '手术' }]
      } else if (this.type === '手术类型') {
        this.tHead = [{ selection: true }, { prop: 'typecode', label: '手术类型编码' }, { prop: 'typename', label: '手术类型' }]
      } else if (this.type === '医疗组') {
        this.tHead = [{ selection: true }, { prop: 'medgroupcode', label: '医疗组编码' }, { prop: 'medgroupname', label: '医疗组' }]
      } else if (this.type === '药品类别') {
        this.tHead = [{ selection: true }, { prop: 'classid', label: '药品类别编码' }, { prop: 'classtitle', label: '药品类别' }]
      } else if (this.type === '自定义药品属性') {
        this.tHead = [{ selection: true }, { prop: 'typecode', label: '自定义药品属性' }, { prop: 'typename', label: '自定义属性名称' }]
      }
      this.getList()
    },
    async getList() {
      let res
      if (this.type === '药品') {
        res = await GetDrug({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '医生') {
        res = await GetDoctor({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '给药途径') {
        res = await GetRoute({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '科室') {
        res = await GetDept({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '疾病') {
        res = await GetDisease({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '手术') {
        res = await GetOperation({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '手术类型') {
        res = await GetOperationType({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '医疗组') {
        res = await GetMedgroup({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '药品类别') {
        res = await GetDrugDnc({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      } else if (this.type === '自定义药品属性') {
        res = await GetDrugType({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          Hospital_Code: this.hospitalCode,
          Filter: this.srm })
      }
      this.tData = res.data.rows
      this.query.total = res.data.records
    },
    show() {
      this.dialogTableVisible = true
    },
    hide() {
      this.dialogTableVisible = false
    },
    searchFun() {
      this.query = {
        page: 1,
        limit: 10,
        total: 0
      }
      this.srm = this.input
      this.getList()
    },
    selectionChange(data) {
      this.selectLists = data
    },
    clearSelect() {
      this.selectLists = []
      this.$refs.graytable.toggleSelection()
    },
    sureFun() {
      if (this.type === '药品') {
        this.$emit('handleGetDrug', this.selectLists)
      } else if (this.type === '医生') {
        this.$emit('handleGetDoctor', this.selectLists)
      } else if (this.type === '给药途径') {
        this.$emit('handleGetRoute', this.selectLists)
      } else if (this.type === '科室') {
        this.$emit('handleGetDept', this.selectLists)
      } else if (this.type === '疾病') {
        this.$emit('handleGetDisease', this.selectLists)
      } else if (this.type === '手术') {
        this.$emit('handleGetOperation', this.selectLists)
      } else if (this.type === '手术类型') {
        this.$emit('handleGetOperationType', this.selectLists)
      } else if (this.type === '医疗组') {
        this.$emit('handleGetMedgroup', this.selectLists)
      } else if (this.type === '药品类别') {
        this.$emit('handleGetDrugDnc', this.selectLists)
      } else if (this.type === '自定义药品属性') {
        this.$emit('handleGetDrugType', this.selectLists)
      }
      this.hide()
    }
    // async blurChange(prop, row) {
    //   const loading = this.$loading({
    // 		  lock: true,
    // 		  text: '正在处理中，请稍后',
    // 		  spinner: 'el-icon-loading',
    // 		  background: 'rgba(0, 0, 0, 0.7)'
    //   })
    //   const res = await this.$ptc.setDeptoutEdit({
    //     deptcode: row.deptcode,
    //     type: this.ctype,
    //     ksdm_encode: row.ksdm_encode,
    //     out_time: row[prop],
    //     ksmc: row.ksmc
    //   })
    //   loading.close()
    //   this.$message({
    // 		  message: res.msg,
    // 		  type: res.code == 1 ? 'success' : 'error',
    // 		  duration: 1500
    //   })
    //   if (res.code == 1) {
    //     this.getList()
    //   }
    // }
  }
}
</script>

<style lang="scss" scoped="scoped">
  .departSearch{
    display: flex;
    .pop_input{
      width: 300px;
    }
    .pop_btn{
      width: 70px;
      height: 40px;
      margin-left: 10px;
    }
  }
</style>
